home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 June (DVD) / DPPRO0605DVD.iso / Install / program files / Borland / BDS / 3.0 / Demos / Delphi.Net / VCL / MRUComboBox / MRUCombos.pas < prev   
Encoding:
Pascal/Delphi Source File  |  2004-10-22  |  4.8 KB  |  195 lines

  1. {                                                                           }
  2. { File:       MRUCombos.pas                                                 }
  3. { Function:   TMRUComboBox component, a ComboBox that adds items entered    }
  4. {             into a most recently used list                                }
  5. { Language:   Delphi 8 for the Micosoft .NET framework                      }
  6. { Author:     Rudy Velthuis                                                 }
  7. {                                                                           }
  8.  
  9. unit MRUCombos;
  10.  
  11. {$IFNDEF CLR}
  12. {$MESSAGE ERROR 'Only in Delphi for the Microsoft .NET framework'}
  13. {$ENDIF}
  14.  
  15. interface
  16.  
  17. uses
  18.   SysUtils, Classes, Controls, StdCtrls;
  19.  
  20. type
  21.   TCustomMRUComboBox = class(TCustomComboBox)
  22.   private
  23.     FMaxCount: Integer;
  24.     FCaseSensitive: Boolean;
  25.     procedure SetMaxCount(const Value: Integer);
  26.   protected
  27.     function GetItemsClass: TCustomComboBoxStringsClass; override;
  28.     procedure DoExit; override;
  29.     procedure DropDown; override;
  30.     property CaseSensitive: Boolean read FCaseSensitive write FCaseSensitive;
  31.     property MaxCount: Integer read FMaxCount write SetMaxCount default 20;
  32.   public
  33.     procedure UpdateContents; virtual;
  34.     constructor Create(AOwner: TComponent); override;
  35.     destructor Destroy; override;
  36.   end;
  37.  
  38.   TMRUComboBox = class(TCustomMRUComboBox)
  39.   published
  40.     property AutoComplete default True;
  41.     property AutoDropDown default False;
  42.     property AutoCloseUp default False;
  43.     property BevelEdges;
  44.     property BevelInner;
  45.     property BevelKind default bkNone;
  46.     property BevelOuter;
  47.     property Style;                     {Must be published before Items}
  48.     property Anchors;
  49.     property BiDiMode;
  50.     property CaseSensitive;
  51.     property CharCase;
  52.     property Color;
  53.     property Constraints;
  54.     property Ctl3D;
  55.     property DragCursor;
  56.     property DragKind;
  57.     property DragMode;
  58.     property DropDownCount;
  59.     property Enabled;
  60.     property Font;
  61.     property ImeMode;
  62.     property ImeName;
  63.     property ItemHeight;
  64.     property ItemIndex default -1;
  65.     property MaxCount default 20;
  66.     property MaxLength;
  67.     property ParentBiDiMode;
  68.     property ParentColor;
  69.     property ParentCtl3D;
  70.     property ParentFont;
  71.     property ParentShowHint;
  72.     property PopupMenu;
  73.     property ShowHint;
  74.     property Sorted;
  75.     property TabOrder;
  76.     property TabStop;
  77.     property Text;
  78.     property Visible;
  79.     property OnChange;
  80.     property OnClick;
  81.     property OnCloseUp;
  82.     property OnContextPopup;
  83.     property OnDblClick;
  84.     property OnDragDrop;
  85.     property OnDragOver;
  86.     property OnDrawItem;
  87.     property OnDropDown;
  88.     property OnEndDock;
  89.     property OnEndDrag;
  90.     property OnEnter;
  91.     property OnExit;
  92.     property OnKeyDown;
  93.     property OnKeyPress;
  94.     property OnKeyUp;
  95.     property OnMeasureItem;
  96.     property OnSelect;
  97.     property OnStartDock;
  98.     property OnStartDrag;
  99.     property Items; { Must be published after OnMeasureItem }
  100.   end;
  101.  
  102. procedure Register;
  103.  
  104. implementation
  105.  
  106. uses
  107.   Windows, Messages, Consts;
  108.  
  109. type
  110.   TMRUComboBoxStrings = class(TCustomComboBoxStrings)
  111.   public
  112.     function Add(const S: string): Integer; override;
  113.     procedure Insert(Index: Integer; const S: string); override;
  114.   end;
  115.  
  116. procedure Register;
  117. begin
  118.   RegisterComponents('Samples', [TMRUComboBox]);
  119. end;
  120.  
  121. { TCustomMRUCombo }
  122.  
  123. constructor TCustomMRUComboBox.Create(AOwner: TComponent);
  124. begin
  125.   inherited;
  126.   FMaxCount := 20;
  127. end;
  128.  
  129. destructor TCustomMRUComboBox.Destroy;
  130. begin
  131.   inherited;
  132. end;
  133.  
  134. procedure TCustomMRUComboBox.DoExit;
  135. begin
  136.   inherited;
  137.   UpdateContents;
  138. end;
  139.  
  140. procedure TCustomMRUComboBox.DropDown;
  141. begin
  142.   inherited;
  143.   UpdateContents;
  144. end;
  145.  
  146. function TCustomMRUComboBox.GetItemsClass: TCustomComboBoxStringsClass;
  147. begin
  148.   Result := TMRUComboBoxStrings;
  149. end;
  150.  
  151. procedure TCustomMRUComboBox.SetMaxCount(const Value: Integer);
  152. begin
  153.   if Value <> FMaxCount then
  154.   begin
  155.     FMaxCount := Value;
  156.     while Value < ItemCount do
  157.       Items.Delete(ItemCount - 1);
  158.   end;
  159. end;
  160.  
  161. procedure TCustomMRUComboBox.UpdateContents;
  162. begin
  163.   if Text <> '' then
  164.     Items.Add(Text);
  165.   ItemIndex := 0;
  166. end;
  167.  
  168. { TMRUComboBoxStrings }
  169.  
  170. function TMRUComboBoxStrings.Add(const S: string): Integer;
  171. var
  172.   Index: Integer;
  173. begin
  174.   Index := IndexOf(S);
  175.   if TCustomMRUComboBox(ComboBox).FCaseSensitive then
  176.     if Strings[Index] <> S then
  177.       Index := -1;
  178.   if Index = -1 then
  179.     Insert(0, S)
  180.   else
  181.     Move(Index, 0);
  182.   Result := 0;
  183. end;
  184.  
  185. procedure TMRUComboBoxStrings.Insert(Index: Integer; const S: string);
  186. begin
  187.   if SendTextMessage(ComboBox.Handle, CB_INSERTSTRING, Index, S) < 0 then
  188.     raise EOutOfResources.Create(SInsertLineError);
  189.   if Count > TCustomMRUComboBox(ComboBox).MaxCount then
  190.     Delete(Count - 1);
  191. end;
  192.  
  193. end.
  194.  
  195.